using System;

namespace Needle.Core.Lifetime {

    /// <summary>
    /// Track an instance until it is garbage collected.
    /// </summary>
    public class TransientAttribute : TrackingPolicyAttribute {

        /// <summary>
        /// Applies the transient tracking policy to the instance.
        /// </summary>
        public override IReference Apply(object instance) {
            return new WeakReference(instance);
        }

        private class WeakReference : IReference {

            System.WeakReference reference;
            public WeakReference(object instance) {
                this.reference = new System.WeakReference(instance);
            }
            #region IReference Members

            public object Target {
                get { return reference.Target; }
            }

            public bool IsValid {
                get { return reference.IsAlive; }
            }

            #endregion
        }

    }
}
